<!DOCTYPE html>
<html>

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <script src="https://cdn.bootcss.com/jquery/3.4.1/jquery.js"></script>
  <link rel="stylesheet" href="/static/css/mini-nord.min.css">
  <link rel="stylesheet" href="/static/css/common.css">
  <link rel="stylesheet" href="/static/css/actionStyle.css">
  <title>fullDress</title>
  <style>
    
  </style>
</head>

<body>

  <div class="container">
    <div class="row">

      <div class="col-md-1 col-lg-2"></div>

      <div class="col-sm-12 col-md-10 col-lg-8">

        <div class="row bootstrap_top">
          <div class="col-sm-2 col-md-2 col-lg-2">
            <div class="bootstrap_top_left">
              <p>青丙拾光</p>
            </div>
          </div>
          <div class="col-sm-10 colh-md-10 col-lg-10">
            <div class="bootstrap_top_right">
              <div class="dropdown">
                <img src="/static/images/home.png" alt="">
                <div class="dropdown-content">
                  <a href="./">Home</a>
                  <a href="./user">HomePage</a>
                </div>
              </div>
              <div id="userinfo" class="userinfo user"></div>
            </div>
          </div>
        </div>

        <div class="row bootstrap_bottom">
          <div class="col-sm-2 col-md-2 col-lg-2 bootstrap_bottom_left">
						<div>管理</div>
						<details>
							<summary>信息管理</summary>
							<ul>
								<li>
									<img src="/static/images/lianjie.png" alt="">
									<a href="/userList">用户信息</a>
								</li>
								<li>
									<img src="/static/images/lianjie.png" alt="">
									<a href="/manager">管理员信息</a>
								</li>
							</ul>
						</details>
						<details>
							<summary>数据管理</summary>
							<ul>
								<li>
									<img src="/static/images/lianjie.png" alt="">
									<!-- <a href="/oath">誓词</a> -->
									<a href="/tips">小贴士</a>
								</li>
								<li>
									<img src="/static/images/lianjie.png" alt="">
									<!-- <a href="/photography">摄影</a> -->
									<a href="/guide">指南</a>
								</li>
								<li>
									<img src="/static/images/lianjie.png" alt="">
									<!-- <a href="/photoSite">摄影地点</a> -->
									<a href="/assort">食谱分类</a>
								</li>
								<li>
									<img src="/static/images/lianjie.png" alt="">
									<!-- <a href="/photoType">摄影类型</a> -->
									<a href="/menu">食谱</a>
								</li>
							</ul>
						</details>
					</div>
          <div class="col-sm-10 colh-md-10 col-lg-10">
            <div class="bootstrap_bottom_right">
              <div class="search">
                <input type="search" id="sea" oninput="tableFilter()" name="keywords" placeholder="请输入关键词" />
              </div>
              <button onclick="msgbox(true)">增加</button>
              <table>
                <thead>
                  <tr>
                    <th>shop_name</th>
                    <th>style_name</th>
                    <th>dress_name</th>
                    <th>操作</th>
                  </tr>
                </thead>
                <tbody id="tb"></tbody>
              </table>
            </div>
          </div>
        </div>

      </div>

      <div class="col-md-1 col-lg-2"></div>

    </div>
  </div>

  <script>

    function getToken() {
      return localStorage.getItem('token') || ''
    }

    function getMyInfo() {
      let uinfo = localStorage.getItem('userinfo')
      if (uinfo) {
        try {
          return JSON.parse(uinfo)
        } catch (err) {
          return null
        }
      }
      return null
    }

    function checkLogin() {
      let tk = getToken()
      if (!tk) {
        location.href = '/login'
        return
      }

      let uinfo = getMyInfo()

      if (uinfo === null) {
        location.href = '/login'
      } else {
        //登录超时
        if (uinfo.time + uinfo.expires < Date.now()) {
          localStorage.removeItem('token')
          localStorage.removeItem('userinfo')
          location.href = '/login'
        }
      }
    }

    //检测是否登录并跳转
    checkLogin()

    function apicall(path, options = {}) {
      let token = getToken()
      let q = '?'
      if (path.indexOf('?') > 0) {
        q = '&'
      }
      path = `${path}${q}token=${token}`

      return fetch(path, options).then(async res => {

        if (!res.ok) {
          let errtext = await res.text()
          throw new Error(`${res.status} : ${errtext}`)
        } else {
          return res.text()
        }

      })

    }

    function getUserInfo(id) {

      return apicall('/user/info/' + id).then(d => {
        let uinfo = JSON.parse(d)
        showUserInfo(uinfo)
      })
        .catch(err => {
          alert(err.message)
        })

    }

    function showUserInfo(u) {
      let html = `
        ${u.nickname} [${u.id}]
        &nbsp;&nbsp;
        <a href="javascript:logout(this);">
          <img src="/static/images/back.png" alt="" style="width: 1.5rem;height: 1.5rem;filter: invert(100%); position: relative; top: 0.4rem;">
        </a>
      `
      document.getElementById('userinfo').innerHTML = html
    }

    function logout() {
      if (!confirm('确定退出？')) {
        return
      }
      localStorage.removeItem('token')
      localStorage.removeItem('userinfo')
      location.href = '/login'
    }

    //getList、writeList
    let list = [];

    fetch('http://10.7.86.197:8080/dress/')
      .then(function (response) {
        return response.json();
      })
      .then(function (myJson) {
        console.log(myJson);
        getList(myJson);
      })
      .catch(err => {
        alert(err.message)
      })

    function getList(myJson) {
      myJson.map((item, index) => {
        list[index] = {
          shop_name: item.shop_name,
          style_name: item.style_name,
          dress_name: item.dress_name,
        }
      })
      showList();
    }

    function showList() {

      let html = ''

      for (let i = 0; i < list.length; i++) {
        html += `
      <tr>
        <td>${list[i].shop_name}</td>
        <td>${list[i].style_name}</td>
        <td>${list[i].dress_name}</td>
        <td>
          <button>更改</button>
          <button>删除</button>
        </td>
      </tr>
    `
      }
      document.getElementById('tb').innerHTML = html
    }

    //search
    function tableFilter() {
      var keyword = document.getElementById("sea").value.toLowerCase().split(" ");
      var table = document.getElementById("tb");
      var ele;
      for (var r = 0; r < table.rows.length; r++) {
        ele = table.rows[r].innerHTML;
        var displayStyle = 'none';
        for (var i = 0; i < keyword.length; i++) {
          if (keyword == "") { displayStyle = ""; break; }
          if (ele.toLowerCase().indexOf(keyword[i]) > 0) {
            displayStyle = '';
          } else {
            displayStyle = 'none';
            break;
          }
        }
        table.rows[r].style.display = displayStyle;
      }
    }

    window.onload = () => {
      getUserInfo('self')
    }

  </script>
</body>

</html>